package com.chb.service.impl;

import com.chb.dao.BranchGoodsDao;
import com.chb.dao.GoodsDao;
import com.chb.dao.OutLibrayTransferDao;
import com.chb.dao.OutLibrayTransferGoodsDao;
import com.chb.model.entity.*;
import com.chb.model.vo.BranchGoodsVo;
import com.chb.model.vo.OutLibrayTransferVo;
import com.chb.model.vo.PageBean;
import com.chb.model.vo.Result;
import com.chb.service.OutLibrayTransferService;
import com.chb.utils.PagingUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author: 陈海彬
 * @date: Created in 2023/10/1 15:59
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class OutLibrayTransferServiceImpl extends PagingUtil implements OutLibrayTransferService {
    @Autowired
    private HttpSession session;
    @Autowired
    private OutLibrayTransferDao outLibrayTransferDao;
    @Autowired
    private OutLibrayTransferGoodsDao outLibrayTransferGoodsDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private BranchGoodsDao branchGoodsDao;

    @Override
    public Result getAllInfo(Integer page, Integer pageSize, Integer branchid) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid(); //获取操作者
        Integer staffBranchid = staff.getBranchid();//获取分店id
        //开启分页
        PageHelper.startPage(page, pageSize);
        List<OutLibrayTransferVo> transferVoList = null;
        //进行查询
        if (branchid != null) {
            //进行模糊查询
            transferVoList = outLibrayTransferDao.selectOutLibrayTransferByBranchId(staffid, branchid, 1);
        } else {
            transferVoList = outLibrayTransferDao.selectOutLibrayTransferByBranchId(staffid, staffBranchid, 0);
        }
        PageInfo<OutLibrayTransferVo> pageInfo = new PageInfo<>(transferVoList);
        PageBean<OutLibrayTransferVo> pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pageBean);
    }

    @Override
    public Result getDetailsByCid(Integer page, Integer pageSize, Integer cid) {
        //设置分页
        PageHelper.startPage(page, pageSize);
        //进行查询
        List<OutLibrayTransferGoods> goodsList = outLibrayTransferGoodsDao.findGoodsByCid(cid);
        //设置返回结果
        PageInfo<OutLibrayTransferGoods> pageInfo = new PageInfo<>(goodsList);
        PageBean<OutLibrayTransferGoods> pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pageBean);
    }

    @Override
    public Result getHorizontalAudit(Integer page, Integer pageSize) {
        Staff staff = (Staff) session.getAttribute("staff");
        //进行dao层的执行
        PageHelper.startPage(page, pageSize);
        List<OutLibrayTransferVo> librayTransferVoList = outLibrayTransferDao.getHorizontalAudit(staff.getBranchid());
        PageInfo<OutLibrayTransferVo> pageInfo = new PageInfo<>(librayTransferVoList);
        PageBean<OutLibrayTransferVo> pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pageBean);
    }

    @Override
    @Transactional
    public Result addOutLibrayTransfer(Integer branchid, List<BranchGoodsVo> branchGoodsVoList) {
        Staff staff = (Staff) session.getAttribute("staff");
        OutLibrayTransfer outLibrayTransfer = new OutLibrayTransfer();
        //进行添加数据
        outLibrayTransfer.setOutstaffid(staff.getStaffid());  //出库人
        outLibrayTransfer.setOuttime(new Date(System.currentTimeMillis()));//出库时间
        outLibrayTransfer.setOutmethod(3);//出库时间
        outLibrayTransfer.setExamstate(1);//审核状态
        outLibrayTransfer.setBranchid(staff.getBranchid());//原始分店
        outLibrayTransfer.setNewbranchid(branchid);//平调分店
        //执行sql语句
        Integer integer = outLibrayTransferDao.addOutLibrayTransfer(outLibrayTransfer);
        //获取平调出库后返回的主键
        Integer cid = outLibrayTransfer.getCid();
        for (BranchGoodsVo branchGoodsVo : branchGoodsVoList) {
            Integer goodsid = branchGoodsVo.getGoodsid();
            System.out.println(goodsid);
            Integer num = branchGoodsVo.getIncounts();
            //查询商品
            Goods goods = goodsDao.selectGoodsByGoodsId(goodsid);
            Double total = goods.getSprice() * num; //计算总价
            //进行赋值操作
            OutLibrayTransferGoods outLibrayTransferGoods = new OutLibrayTransferGoods();
            outLibrayTransferGoods.setCid(cid);//设置cid
            outLibrayTransferGoods.setGoodsid(goodsid);//商品id
            outLibrayTransferGoods.setNum(num);//商品数量
            outLibrayTransferGoods.setTotal(total);//设置总价
            //执行sql语句
            outLibrayTransferGoodsDao.addOutLibrayTransferGoods(outLibrayTransferGoods);
        }
        return Result.success();
    }

    @Override
    @Transactional
    public Result updateOutLibrayTransfer(Integer cid, Integer status, String remarks) {
        Staff staff = (Staff) session.getAttribute("staff");

        OutLibrayTransfer outLibrayTransfer = new OutLibrayTransfer();
        outLibrayTransfer.setExamstaffid(staff.getStaffid());//审核人
        outLibrayTransfer.setExamtime(new Date(System.currentTimeMillis()));//审核时间
        outLibrayTransfer.setExamstate(2);//审核状态
        outLibrayTransfer.setCid(cid);//设置修改的id

        //进行平调操作
        if (status.equals(1)) {
            //查询库存
            List<OutLibrayTransferGoods> goodsList = outLibrayTransferGoodsDao.findBranchidByCid(cid);
            for (OutLibrayTransferGoods outLibrayTransferGoods : goodsList) {
                Integer num = outLibrayTransferGoods.getNum(); //获取要平调的数量
                Integer branchid = outLibrayTransferGoods.getBranchid();//平调的分店id
                Integer goodsid = outLibrayTransferGoods.getGoodsid();//平调的商品id

                //获取原来门店下的库存
                StorageVolume storageVolume = new StorageVolume();
                storageVolume.setBranchid(branchid);
                storageVolume.setGoodsid(goodsid);
                BranchGoods librayByNow = branchGoodsDao.findInLibrayByNow(storageVolume);

                Integer counts = librayByNow.getCounts();//原先门店下的库存
                if (counts < num) {
                    return new Result(202, "平调商品数量超过原始分店数量，请驳回该平调！", null);
                } else {
                    //通过审核
                    outLibrayTransferDao.updateOutLibrayTransfer(outLibrayTransfer);
                    //修改原先分店商品数量
                    int nowNum = counts - num;//原先分店商品数量
                    librayByNow.setCounts(nowNum);
                    branchGoodsDao.updateCounts(librayByNow);


                    //获取当前门店下商品的数量
                    StorageVolume storageVolume1 = new StorageVolume();
                    storageVolume1.setBranchid(staff.getBranchid());
                    storageVolume1.setGoodsid(goodsid);
                    BranchGoods inLibrayByNow = branchGoodsDao.findInLibrayByNow(storageVolume1);
                    System.out.println(inLibrayByNow);
                    if (inLibrayByNow != null) {
                        //添加平调分店商品数量
                        Integer counts1 = inLibrayByNow.getCounts();
                        inLibrayByNow.setCounts(counts1 + num);
                        branchGoodsDao.updateCounts(inLibrayByNow);
                    } else {
                        inLibrayByNow = new BranchGoods();
                        //没有该商品进行添加商品
                        inLibrayByNow.setCounts(num);//设置商品数量
                        inLibrayByNow.setGoodsid(goodsid);
                        inLibrayByNow.setBranchid(staff.getBranchid());
                        branchGoodsDao.addBranchGoods(inLibrayByNow);
                    }
                }
            }
        } else if (status.equals(2)) {//驳回检查
            outLibrayTransfer.setExamstate(3);//审核驳回
            outLibrayTransfer.setRemarks(remarks);//驳回备注
            //执行sql
            outLibrayTransferDao.updateOutLibrayTransfer(outLibrayTransfer);
        }
        return Result.success();
    }
}